package cn.monkey.family.server.domain.uc.org;

import cn.monkey.family.data.Result;
import cn.monkey.family.data.context.Context;
import cn.monkey.family.data.context.RequestContext;
import cn.monkey.family.data.uc.org.OrgQueryRequest;
import cn.monkey.family.data.uc.org.Organization;
import cn.monkey.family.data.uc.org.OrganizationVo;
import com.google.common.base.Strings;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("org")
public class OrganizationController {
    private final IOrganizationService organizationService;

    public OrganizationController(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    @GetMapping({"{id}", ""})
    Result<?> get(@Context RequestContext requestContext,
                  @PathVariable(value = "id", required = false) String id,
                  OrgQueryRequest queryRequest,
                  Pageable pageable) {
        if (!Strings.isNullOrEmpty(id)) {
            return this.organizationService.readById(requestContext, id);
        }
        if (pageable.isUnpaged()) {
            return this.organizationService.readAll(requestContext, queryRequest, pageable.getSort());
        }
        return this.organizationService.readPage(requestContext, queryRequest, pageable);
    }

    @PostMapping
    public Result<OrganizationVo> create(@Context RequestContext requestContext,
                                         @RequestBody Organization organization) {
        return this.organizationService.create(requestContext, organization);
    }

    @PostMapping("{id}")
    public Result<OrganizationVo> update(@Context RequestContext requestContext,
                                         @PathVariable String id,
                                         @RequestBody Organization organization) {
        return this.organizationService.update(requestContext, id, organization);
    }

    @DeleteMapping("{id}")
    public Result<Void> delete(@Context RequestContext requestContext,
                               @PathVariable String id) {
        return this.organizationService.delete(requestContext, id);
    }

}
